package com.ycx.ycxmybatismanagement.controller;

import com.ycx.ycxmybatismanagement.dto.StudentDTO;
import com.ycx.ycxmybatismanagement.dto.StudentListDTO;
import com.ycx.ycxmybatismanagement.pojo.PageBean;
import com.ycx.ycxmybatismanagement.pojo.Result;
import com.ycx.ycxmybatismanagement.service.StudentService;
import com.ycx.ycxmybatismanagement.vo.StudentListVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/students")
public class StudentController {
    @Autowired
    private StudentService studentService;

    /**
     * 学生分页
     * @param studentListDTO
     * @return
     */
    @GetMapping
    public Result<PageBean<StudentListVO>> studentPageList(StudentListDTO studentListDTO){
        PageBean<StudentListVO> studentPageList =  studentService.studentPageList(studentListDTO);
        return Result.success(studentPageList);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("{ids}")
    public Result deleteStudentListByIds(@PathVariable("ids") Integer[] ids){
        studentService.deleteStudentListByIds(ids);
        return Result.success();
    }

    /**
     * 添加
     * @param studentDTO
     * @return
     */
    @PostMapping
    public Result addStudent(@RequestBody StudentDTO studentDTO){
        studentService.addStudent(studentDTO);
        return Result.success();

    }

    /**
     * 回显
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Result<StudentListVO> selectStudentById(@PathVariable("id")Integer id){
       StudentListVO studentVo = studentService.selectStudentById(id);
       return Result.success(studentVo);
    }

    /**
     * 修改
     * @param studentDTO
     * @return
     */
    @PutMapping
    public Result updateStudentById(@RequestBody StudentDTO studentDTO){
        studentService.updateStudentById(studentDTO);
        return Result.success();
    }

    @PutMapping("violation/{id}/{score}")
    public Result updateStudentScore(@PathVariable("id")Integer id,@PathVariable("score") Integer score){
        studentService.updateStudentScore(id,score);
        return Result.success();
    }

}
